package pagefetch.model;

import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStreamReader;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HTMLPage {


	private ArrayList<HTMLPage> subpages;
	private ArrayList<String> content, images, css, links; 
	private static ArrayList<HandledLinkObject> visitedLinks = new ArrayList<HandledLinkObject>();;
	private static URL startURL;

	private String name = "0";

	private HTMLPage() {
		subpages = new ArrayList<HTMLPage>();
		content = new ArrayList<String>();
		images = new ArrayList<String>();
		css = new ArrayList<String>();
		links = new ArrayList<String>();
		
	}
	
	public static HTMLPage getPage(String site, int localDepth){
		try {
			String newLink = "index.html";
			if(!site.endsWith("/")) site +="/";
			startURL = new URL(site);
			System.out.println("old: " + site + " new: " + getNewLink(site));
			//visitedLinks.add(new HandledLinkObject(startURL, newLink));
			
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		HTMLPage page =  getPage(startURL, localDepth, 0);
		
		int size =  visitedLinks.size();
		
		for(int i = 1; i < size; i++){
			if(!visitedLinks.get(i).getNewLink().contains(".")){
				getPage(visitedLinks.get(i).getOldLink(), localDepth-1, 0);
			}
			size = visitedLinks.size();
		}
		
		return page;
	}

	private static HTMLPage getPage(URL site, int localDepth,
			int externalDepth) {
		HTMLPage newPage = new HTMLPage();

		try {
			URLConnection connection = (site).openConnection();
			BufferedReader pageIn = new BufferedReader(new InputStreamReader(
					connection.getInputStream()));

			String inline;

			while ((inline = pageIn.readLine()) != null) {
				inline = newPage.scanForLink(inline);
				newPage.scanForCss(inline);
				newPage.scanForImage(inline);
				newPage.scanForScript(inline);
				newPage.content.add(inline);
			}

		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block//
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	//for (String s : newPage.links) {
		//	System.out.println(s);
		//	
	//	}
		//System.out.println("Unique pages: " + visitedLinks.size());
		
		return newPage;
	}

	private void scanForCss(String inline) {
		Pattern pattern = 
				Pattern.compile("http.*?\\.css", Pattern.CASE_INSENSITIVE);

		Matcher matcher = pattern.matcher(inline);

		while (matcher.find()) {
			String s = matcher.group();
			
			
			String newlink = getNewLink(s);
			css.add(newlink);
			System.out.println("old: " + s + " new: " + newlink);
		}
	}

	private void scanForImage(String inline) {
		Pattern pattern = 
				Pattern.compile("<img src=.*?\".*?\"",	Pattern.CASE_INSENSITIVE);

		Matcher matcher = pattern.matcher(inline);

		while (matcher.find()) {
			String s = matcher.group();
			s = s.substring(s.indexOf('h'), s.length() - 1);
			
			String newlink = getNewLink(s);
			images.add(newlink);
			System.out.println("old: " + s + " new: " + newlink);
		}
	}

	private String scanForLink(String inline) {
		Pattern pattern = 
				Pattern.compile("a href.*?\".*?\"", Pattern.CASE_INSENSITIVE);

		Matcher matcher = pattern.matcher(inline);
		
		
		
		while (matcher.find()) {
			String s = matcher.group();
			
			s = s.substring(s.indexOf('"')+1, s.length() - 1);
			//links.add(s);
			
			String newlink = getNewLink(s);
			inline.replaceAll("\\Q" + s + "\\E", "\\Q" + newlink + "\\E");
			links.add(newlink);
			System.out.println("old: " + s + " new: " + newlink);
			
			
		}
		return inline;
	}

	private void scanForScript(String inline) {

	}

	private static void handleJSLink(String inline) {
		// TODO Auto-generated method stub

	}

	private static void handleCSSLink(String inline) {
		// TODO Auto-generated method stub

	}

	private static void handleImageLink(String inline) {
		// TODO Auto-generated method stub

	}

	private static void handlePageLink(String inline, int localDepth,
			int externalDepth) {
		// TODO Auto-generated method stub

	}



	public void saveToFile() {

	}

	public void print() {
		for (String s : content) {
			System.out.println(s);
		}
	}

	public ArrayList<String> getContent() {
		return content;
	}
	
	public static String getNewLink(String link){
		
		
		
		URL oldUrl;
		try {
			oldUrl = new URL(startURL, link);
			if(oldUrl.getHost().equals(startURL.getHost())){
				
				for(HandledLinkObject h : visitedLinks){
					if(h.sameAs(oldUrl)){
						return h.getNewLink();
					}
				}
				String newLink = "";
				if(visitedLinks.size() == 0) newLink = "index.html";
				
				else newLink = "" + visitedLinks.size() + link.substring(Math.max(link.lastIndexOf("."), link.lastIndexOf('/')+1));
				visitedLinks.add(new HandledLinkObject(oldUrl, newLink));
				return newLink;
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return link;
		
		
		
	}

}
